/*
 * Diagrama_ToolBar.java
 *
 * Created on 20 de Junho de 2007, 17:44
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface.JGraph_Algebra;

import java.awt.event.ActionEvent;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JToolBar;

import org.jerm.Interface.Principal;
import org.jgraph.JGraph;

/**
 *
 * @author ra044637
 */
public class Diagrama_ToolBar extends JToolBar {

    private Diagrama_JGraph graph;
    private Principal principal;
    
    private double pX;
    private double pY;
    
    private int countRelacao;
    
    protected AbstractAction relacao;
    protected AbstractAction pi;
    protected AbstractAction sigma;
    protected AbstractAction ro;
    protected AbstractAction produto;
    protected AbstractAction join;
    protected AbstractAction screenshot;
    protected AbstractAction executa;
    
    private Consulta c_principal;
    
    /** Creates a new instance of Diagrama_ToolBar */
    public Diagrama_ToolBar(Diagrama_JGraph graph, Principal Principal) {
        super();
        this.setFloatable(false);
        this.setRollover(true);
        this.graph = graph;
        
        this.insertButtons();
        
        this.principal = Principal;
        
        pX = pY = 10;
        countRelacao = 0;
    }
    
    public void insertButtons()
    {
        URL iconUrl;
        ImageIcon imageIcon;
        
        
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/relacao.png");
        imageIcon = new ImageIcon(iconUrl);
        relacao=new AbstractAction("", imageIcon) {
                public void actionPerformed(ActionEvent e) {
                    addRelacao();
                }
        };
        this.add(relacao);
        relacao.setEnabled(true);
        
	this.addSeparator();
        
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/projecao.png");
        imageIcon = new ImageIcon(iconUrl);
        pi=new AbstractAction("", imageIcon) {
                public void actionPerformed(ActionEvent e) {
                    addProjecao();
                }
        };
        this.add(pi);
        pi.setEnabled(false);
        
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/selecao.png");
        imageIcon = new ImageIcon(iconUrl);
        sigma=new AbstractAction("", imageIcon) {
                public void actionPerformed(ActionEvent e) {
                    addSelecao();
                }
        };
        this.add(sigma);
        sigma.setEnabled(false);
        
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/renomeacao.png");
        imageIcon = new ImageIcon(iconUrl);
        ro=new AbstractAction("", imageIcon) {
                public void actionPerformed(ActionEvent e) {
                    addRenomeacao();
                }
        };
        this.add(ro);
        ro.setEnabled(false);
        
	this.addSeparator();
        
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/produto.png");
        imageIcon = new ImageIcon(iconUrl);
        produto=new AbstractAction("", imageIcon) {
                public void actionPerformed(ActionEvent e) {
                    addProduto();
                }
        };
        this.add(produto);
        produto.setEnabled(false);
        
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/juncao.png");
        imageIcon = new ImageIcon(iconUrl);
        join=new AbstractAction("", imageIcon) {
                public void actionPerformed(ActionEvent e) {
                    addJuncao();
                }
        };
        this.add(join);
        join.setEnabled(false);
        
        this.addSeparator();
               
        // Extra
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/screenshot.png");
        imageIcon = new ImageIcon(iconUrl);
        screenshot=new AbstractAction("", imageIcon) {
                    public void actionPerformed(ActionEvent e) {
                            ((MyGraph)graph.getGraph()).exportImage();
                    }
        };
        this.add(screenshot);
        screenshot.setEnabled(true);
        
        // Extra
        iconUrl = getClass().getClassLoader().getResource("org/jerm/Interface/Icones/ok.png");
        imageIcon = new ImageIcon(iconUrl);
        executa=new AbstractAction("", imageIcon) {
                    public void actionPerformed(ActionEvent e) {
                        executaSQL();
                        principal.Abas.setSelectedIndex(3);
                    }
        };
        this.add(executa);
        screenshot.setEnabled(true);
        
    }
    
    public void executaSQL()
    {
        AlgebraToSQL alg2sql = new AlgebraToSQL(c_principal);
        principal.getConsultas().getTabela().Exibe(alg2sql.getSQL());
    }
    
    public void addRelacao()
    {
        Relacao r = new Relacao("Relacao "+(new Integer(++countRelacao)).toString(), pX, pY, 100, 30);
        Consulta c = new Consulta(r);
        r.setConsultaP(c);
        graph.insere(c);
        pY += 10;
        pX += 10;
        
        c_principal = c;
    }
    
    public void addProjecao()
    {
        Consulta c_filha = getConsulta(graph.getGraph().getSelectionCell());

        Projecao p = new Projecao(c_filha, pX, pY, 150, 40, "<Edite os atributos>");
        Consulta c = new Consulta(p);
        p.setConsultaP(c);
        graph.insere(c);
        
        c_principal = c;
    }
    
    public void addRenomeacao()
    {
        Consulta c_filha = getConsulta(graph.getGraph().getSelectionCell());

        Renomeacao r = new Renomeacao(c_filha, pX, pY, 150, 40, "<Edite os nomes>");
        Consulta c = new Consulta(r);
        r.setConsultaP(c);
        graph.insere(c);
        
        c_principal = c;
    }
    
    public void addSelecao()
    {
        Consulta c_filha = getConsulta(graph.getGraph().getSelectionCell());

        Selecao s = new Selecao(c_filha, pX, pY, 150, 40, "<Edite as condicoes>");
        Consulta c = new Consulta(s);
        s.setConsultaP(c);
        graph.insere(c);
        
        c_principal = c;
    }
    
    public void addProduto()
    {
        Consulta c1 = null;
        Consulta c2 = null;
        
        Object[] cell = graph.getGraph().getSelectionCells();
        
        if(cell.length == 2)
        {
            c1 = getConsulta(cell[0]);
            c2 = getConsulta(cell[1]);
        }
        
        Produto p = new Produto(c1, c2, pX, pY, 30, 25);
        Consulta c = new Consulta(p);
        p.setConsultaP(c);
        graph.insere(c);
        
        c_principal = c;
    }

    public void addJuncao()
    {
        Consulta c1 = null;
        Consulta c2 = null;
        
        Object[] cell = graph.getGraph().getSelectionCells();
        
        if(cell.length == 2)
        {
            c1 = getConsulta(cell[0]);
            c2 = getConsulta(cell[1]);
        }
        
        Juncao j = new Juncao(c1, c2, pX, pY, 140, 40, "<Edite as condicoes>");
        Consulta c = new Consulta(j);
        j.setConsultaP(c);
        graph.insere(c);
        
        c_principal = c;
    }
    
    private Consulta getConsulta(Object obj)
    {
        Consulta c = null;
        if(obj.getClass().getName() == "Interface.JGraph_Algebra.Relacao")
        {
            c = ((Relacao) obj).getConsultaP();
        }
        else if(obj.getClass().getName() == "Interface.JGraph_Algebra.Projecao")
        {
            c = ((Projecao) obj).getConsultaP();
        }
        else if(obj.getClass().getName() == "Interface.JGraph_Algebra.Renomeacao")
        {
            c = ((Renomeacao) obj).getConsultaP();
        }
        else if(obj.getClass().getName() == "Interface.JGraph_Algebra.Selecao")
        {
            c = ((Selecao) obj).getConsultaP();
        }
        else if(obj.getClass().getName() == "Interface.JGraph_Algebra.Produto")
        {
            c = ((Produto) obj).getConsultaP();
        }
        else if(obj.getClass().getName() == "Interface.JGraph_Algebra.Juncao")
        {
            c = ((Juncao) obj).getConsultaP();
        }
        
        return c;
    }
    
}
